//
//  EasyGlyph.m
//  EasyGlyph 1.2
//
//  Created by Alexei White on 14/05/09.
//  Copyright 2009 Alexei White. All rights reserved.
//

#import "EasyGlyph.h"

int const EG_ORIENTATION_LEFT = 0;
int const EG_ORIENTATION_RIGHT = 1;
int const EG_ORIENTATION_TOP = 2;
int const EG_ORIENTATION_BOTTOM = 3;
int const EG_ORIENTATION_CENTERED = 4;

@implementation EasyGlyph

// set things up
-(void)initFont:(NSString *)fontImage:(NSString *)charSt:(NSString *)fontInfo {
	fontUIImage = [UIImage imageNamed:fontImage];
	characterDict = [[[NSMutableDictionary alloc] init] retain];
	charSet = [[NSString alloc] initWithString:charSt];
	charSetLength = [charSet length];
	NSArray *chunks = [fontInfo componentsSeparatedByString: @";"];
	for (int i = 0; i < charSetLength; i++) {
		NSString *ch = [[NSString alloc] initWithString:[charSet substringWithRange:NSMakeRange(i, 1)]];
		EasyGlyphChar *cf = [[[EasyGlyphChar alloc] init] retain];
		NSString *ich = [[NSString alloc] initWithString:[chunks objectAtIndex:i]];
		NSArray *dta = [ich componentsSeparatedByString: @","];
		cf.x = [[dta objectAtIndex:0] floatValue];
		cf.y = [[dta objectAtIndex:1] floatValue];
		cf.w = [[dta objectAtIndex:2] floatValue];
		cf.h = [[dta objectAtIndex:3] floatValue];
		cf.kernx1 = [[dta objectAtIndex:4] floatValue];
		cf.kernx2 = [[dta objectAtIndex:5] floatValue];
		[characterDict setObject:cf forKey:ch];
		[ch release];
		[ich release];
	}
	
	[fontImage release];
	[charSt release];
	[fontInfo release];
}

// Write the text to a texture
-(UIImage *)writeTextToTexture:(NSString *)lettering {
	
	float stringWidth = 0.0f;
	float stringHeight = 0.0f;
	float lastTotalWidth = 0.0f;
	
	for (int i = 0; i < [lettering length]; i++) {
		NSString *ch = [[NSString alloc] initWithString:[lettering substringWithRange:NSMakeRange(i, 1)]];
		if ([ch compare:@" "] == NSOrderedSame ) {
			EasyGlyphChar *cf = [characterDict objectForKey:@"c"];
			stringWidth += cf.kernx2-cf.kernx1;
			stringHeight = cf.h;
			lastTotalWidth = cf.w;
		} else {
			EasyGlyphChar *cf = [characterDict objectForKey:ch];
			
			if (i > 0)
				stringWidth -= cf.kernx1;
			stringHeight = cf.h;
			stringWidth += cf.kernx2;
			lastTotalWidth = cf.w;
		}
		[ch release];
	}	
	stringWidth += lastTotalWidth;
	
	UIGraphicsBeginImageContext(CGSizeMake(stringWidth,stringHeight));
	
	float xPos = 0.0f;
	for (int i = 0; i < [lettering length]; i++) {
		NSString *ch = [[NSString alloc] initWithString:[lettering substringWithRange:NSMakeRange(i, 1)]];
		if ([ch compare:@" "] == NSOrderedSame ) {
			EasyGlyphChar *cf = [characterDict objectForKey:@"c"];
			xPos += cf.kernx2-cf.kernx1;
			
		} else {
			EasyGlyphChar *cf = [characterDict objectForKey:ch];
			int x = (int)cf.x;
			int y = (int)cf.y;
			int w = (int)cf.w;
			int h = (int)cf.h;
			CGImageRef imageRef = CGImageCreateWithImageInRect([fontUIImage CGImage], CGRectMake(x,y,w,h));
			UIImage *croppedLetter = [UIImage imageWithCGImage:imageRef];
			if (i > 0)
				xPos -= cf.kernx1;
			[croppedLetter drawInRect:CGRectMake(xPos, 0, w, h)];
			xPos += cf.kernx2;
			
			CGImageRelease(imageRef);
			
		}
		[ch release];
	}	
	UIImage *final = UIGraphicsGetImageFromCurrentImageContext();
	[final retain];
	UIGraphicsEndImageContext();
	
	[final autorelease];
	return final;
}


// Crop an image
+(UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect {
	UIGraphicsBeginImageContext(rect.size);
	CGContextRef currentContext = UIGraphicsGetCurrentContext();
	
	CGRect clippedRect = CGRectMake(0,0,rect.size.width,rect.size.height);
	CGContextClipToRect(currentContext,clippedRect);
	
	CGRect drawRect = CGRectMake(rect.origin.x * -1, rect.origin.y*-1, imageToCrop.size.width, imageToCrop.size.height);
	
	CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);
	
	UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
	
	UIGraphicsEndImageContext();
	
	return cropped;
}


// Get an aspect-correct CGRect from a UIImage
+(CGRect)getRectFromTargetWidth:(CGFloat)texWidth:(CGFloat)texHeight:(CGFloat)targetWidth:(CGPoint)centerPos:(int)hOrientation:(int)vOrientation {
    
	CGFloat newHeight = (texHeight / texWidth) * targetWidth;
	
	CGRect fRect = CGRectMake(centerPos.x, centerPos.y, targetWidth, newHeight);
	
	if (hOrientation == EG_ORIENTATION_RIGHT) {
		fRect.origin.x = centerPos.x-targetWidth;
	} else if (hOrientation == EG_ORIENTATION_CENTERED) {
		fRect.origin.x = centerPos.x-(targetWidth/2.0f);
	}
    
	if (vOrientation == EG_ORIENTATION_BOTTOM) {
		fRect.origin.y = centerPos.y-newHeight;
	} else if (vOrientation == EG_ORIENTATION_CENTERED) {
		fRect.origin.y = centerPos.y-(newHeight/2.0f);
	}
	
	return fRect;
}

// Get an aspect-correct CGRect from a UIImage
+(CGRect)getRectFromTargetHeight:(CGFloat)texWidth:(CGFloat)texHeight:(CGFloat)targetHeight:(CGPoint)centerPos:(int)hOrientation:(int)vOrientation {
	
	CGFloat newWidth = (texWidth / texHeight) * targetHeight;
	
	CGRect fRect = CGRectMake(centerPos.x, centerPos.y, newWidth, targetHeight);
	
	if (hOrientation == EG_ORIENTATION_RIGHT) {
		fRect.origin.x = centerPos.x-newWidth;
	} else if (hOrientation == EG_ORIENTATION_CENTERED) {
		fRect.origin.x = centerPos.x-(newWidth/2.0f);
	}
	
	if (vOrientation == EG_ORIENTATION_BOTTOM) {
		fRect.origin.y = centerPos.y-targetHeight;
	} else if (vOrientation == EG_ORIENTATION_CENTERED) {
		fRect.origin.y = centerPos.y-(targetHeight/2.0f);
	}
	
	return fRect;
}

// Tear down
-(void)dealloc {
	for (int i = 0; i < charSetLength; i++) {
		[[characterDict objectForKey:[charSet substringWithRange:NSMakeRange(i, 1)]] release];
	}
	[fontUIImage release];
	[characterDict release];
	[charSet release];
	[super dealloc];
}

@end
